/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.SdBountyHunter.Listeners;

import com.SyDeck.sdCities.CityInfo;
import com.SySammy.SdBountyHunter.Handlers.BountyHandler;
import com.SySammy.SdBountyHunter.Prop.BountyHunterProperties;
import com.SySammy.SdBountyHunter.sdLaw;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerTeleportEvent;

public class PListener extends PlayerListener {

    sdLaw plugin;

    public PListener(sdLaw aThis) {
        this.plugin = aThis;
    }

    @Override
    public void onPlayerTeleport(PlayerTeleportEvent event) {
        final Player p = event.getPlayer();
        BountyHandler BH = new BountyHandler(plugin);
        if (BH.GetCaught(p.getName())) {
            p.sendMessage(ChatColor.RED + "You are under arrest, do your time and don't ever come back.");
            p.sendMessage(ChatColor.DARK_AQUA + "The arrest time on the server is: " + ChatColor.GOLD + BountyHunterProperties.arrestTime + " minutes");
            event.setCancelled(true);
        }
    }

    @Override
    public void onPlayerInteract(PlayerInteractEvent event) {
        final Player p = event.getPlayer();
        BountyHandler BH = new BountyHandler(plugin);
        if (BH.GetCaught(p.getName())) {
            if (event.getClickedBlock().getType().equals(Material.STONE_BUTTON)) {
                p.sendMessage(ChatColor.RED + "You are under arrest, do your time and don't ever come back.");
                p.sendMessage(ChatColor.DARK_AQUA + "The arrest time on the server is: " + ChatColor.GOLD + BountyHunterProperties.arrestTime + " minutes");
                event.setCancelled(true);
            }
            return;
        }
        if (event.getAction().equals(Action.LEFT_CLICK_BLOCK)) {

            PrisonMaker PM = new PrisonMaker(plugin);
            if (PM.CanLeftClickL(p)) {
                CityInfo ci = new CityInfo();
                String citycheck = ci.CityNameByBlock(event.getClickedBlock().getX(), event.getClickedBlock().getZ());
                if (citycheck == null) {
                    p.sendMessage(ChatColor.RED + "The prison must be inside your city.");
                    return;
                }
                if (!ci.izOwner(p.getName())) {
                    p.sendMessage(ChatColor.RED + "The prison must be inside your city.");
                    return;
                }
                if (!ci.GetCityName(p.getName()).equalsIgnoreCase(citycheck)) {
                    p.sendMessage(ChatColor.RED + "The prison must be inside your city.");
                    return;
                }

                if (!event.getClickedBlock().getType().equals(Material.AIR) && !event.getClickedBlock().getType().equals(Material.SIGN) && !event.getClickedBlock().getType().equals(Material.SIGN_POST) && !event.getClickedBlock().getType().equals(Material.CHEST)) {

                    PM.RegPrisonLocation(p, event.getClickedBlock().getLocation());
                    return;

                }
            }

            if (PM.CanLeftClickS(p)) {
                CityInfo ci = new CityInfo();
                String citycheck = ci.CityNameByBlock(event.getClickedBlock().getX(), event.getClickedBlock().getZ());
                if (citycheck == null) {
                    p.sendMessage(ChatColor.RED + "The prison Sign must be inside your city.");
                    return;
                }

                if (!ci.izOwner(p.getName())) {
                    p.sendMessage(ChatColor.RED + "The prison Sign must be inside your city.");
                    return;
                }
                if (!ci.GetCityName(p.getName()).equalsIgnoreCase(citycheck)) {
                    p.sendMessage(ChatColor.RED + "The prison Sign must be inside your city.");
                    return;
                }
                if (event.getClickedBlock().getType().equals(Material.SIGN) || event.getClickedBlock().getType().equals(Material.SIGN_POST) || event.getClickedBlock().getType().equals(Material.WALL_SIGN)) {
                    PM.RegSign(p, event.getClickedBlock().getLocation());
                    return;
                }
            }
            if (PM.CanLeftClickLC(p)) {
                CityInfo ci = new CityInfo();
                String citycheck = ci.CityNameByBlock(event.getClickedBlock().getX(), event.getClickedBlock().getZ());
                if (citycheck == null) {
                    p.sendMessage(ChatColor.RED + "The prison Chest must be inside your city.");
                    return;
                }

                if (!ci.izOwner(p.getName())) {
                    p.sendMessage(ChatColor.RED + "The prison Chest must be inside your city.");
                    return;
                }
                if (!ci.GetCityName(p.getName()).equalsIgnoreCase(citycheck)) {
                    p.sendMessage(ChatColor.RED + "The prison Chest must be inside your city.");
                    return;
                }
                if (event.getClickedBlock().getType().equals(Material.CHEST)) {
                    PM.RegChestL(p, event.getClickedBlock().getLocation());
                    return;
                }
            }
            if (PM.CanLeftClickRC(p)) {
                CityInfo ci = new CityInfo();
                String citycheck = ci.CityNameByBlock(event.getClickedBlock().getX(), event.getClickedBlock().getZ());
                if (citycheck == null) {
                    p.sendMessage(ChatColor.RED + "The prison Chest must be inside your city.");
                    return;
                }

                if (!ci.izOwner(p.getName())) {
                    p.sendMessage(ChatColor.RED + "The prison Chest must be inside your city.");
                    return;
                }
                if (!ci.GetCityName(p.getName()).equalsIgnoreCase(citycheck)) {
                    p.sendMessage(ChatColor.RED + "The prison Chest must be inside your city.");
                    return;
                }
                if (event.getClickedBlock().getType().equals(Material.CHEST)) {
                    PM.RegChestR(p, event.getClickedBlock().getLocation());
                    return;
                }
            }
        }
    }
}
